package restPROC;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;

import dao.daoKonekcija;
import dao.daoLinija;
import dao.daoMapa;
import dao.daoPermisije;
import utilClasses.ListaLinija;
import utilClasses.ListaMapa;


@Path("/mape")
public class GoogleMape {
	@GET
	@Path("/get")
	@Produces({"application/xml"})
	public ListaMapa getMape()
	{
		ListaMapa rez=new ListaMapa();
		try {
			
			rez=daoMapa.sveMape();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return rez;
	}
	@GET
	@Path("/dajLinije")
	@Produces({"application/xml"})
	public ListaLinija getLinije(@QueryParam("id") String id)
	{
		ListaLinija rez=new ListaLinija();
		try {
			
			rez=daoLinija.dajLinije(id);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return rez;
	}
	@GET
	@Path("/update")
	@Produces({"plain/text"})
	public String update(@QueryParam("id") String id,@QueryParam("lat") String lat,@QueryParam("lgt") String lgt,@QueryParam("zoom") String zoom,@Context HttpServletRequest req)
	{
		HttpSession sess = req.getSession(true);
		String reqkorisnik=sess.getAttribute("korisnik").toString();
		System.out.println(reqkorisnik);
		try {
			boolean jelima=daoPermisije.provjeri(reqkorisnik, "modmapa");
			System.out.println(jelima);
			if(!jelima) return null;
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String rez=new String();
		try {
			
			daoMapa.updateMapa(Integer.parseInt(id), Double.parseDouble(lat),Double.parseDouble(lgt),Double.parseDouble(zoom));
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
			return e.getMessage();
		}
		
		return "Uspjesno";
	}
	@GET
	@Path("/insert")
	@Produces({"plain/text"})
	public String insert(@QueryParam("lat") String lat,@QueryParam("lgt") String lgt,@QueryParam("zoom") String zoom,@Context HttpServletRequest req)
	{
		HttpSession sess = req.getSession(true);
		String reqkorisnik=sess.getAttribute("korisnik").toString();
		System.out.println(reqkorisnik);
		try {
			boolean jelima=daoPermisije.provjeri(reqkorisnik, "modmapa");
			System.out.println(jelima);
			if(!jelima) return null;
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String rez=new String("Uspjesno");
		try {
			
			daoMapa.insertGoogleMapa( Double.parseDouble(lat),Double.parseDouble(lgt),Double.parseDouble(zoom));
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
			return e.getMessage();
		}
		
		return rez;
	}
	
}
